<template>
  <el-form :inline="true" :model="form" ref="form">
    <el-form-item label="日期" prop="date">
      <el-date-picker
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        type="daterange"
        v-model="form.date"
        value-format="yyyy-MM-dd"
        :picker-options="pickerOptions"
        allowClear
      />
    </el-form-item>
    <el-form-item>
      <el-tooltip class="item" effect="dark" content="查询" placement="top">
        <el-button type="primary" icon="el-icon-search" @click.stop="handleSearch" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="重置" placement="top">
        <el-button @click="handleReset" icon="el-icon-refresh-right" />
      </el-tooltip>
    </el-form-item>
    <el-form-item class="fr">
      <el-tooltip class="item" effect="dark" content="新增盘点计划" placement="top">
        <el-button
          icon="el-icon-plus"
          v-if="$auth('assets:add')"
          type="primary"
          @click="handleEdit(false, {})"
        />
      </el-tooltip>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        date: null,
        pageNo: 1
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '年初至今',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              const year = start.getFullYear()
              start.setTime(new Date(`${year}-1-1`))
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  methods: {
    handleSearch() {
      if (this.form.date) {
        this.$emit('search', { startTime: this.form.date[0], endTime: this.form.date[1] })
      } else {
        this.$message.error('请选择时间范围')
      }
    },
    handleReset() {
      this.form.date = null
      this.$emit('search', this.form)
    },
    handleEdit(bool, obj) {
      this.$router.push({
        name: 'assetsPlanCreate',
        params: { obj, bool }
      })
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 0;
}
</style>
